package cn.net.sinodata.system.msghandler.parse.scripthandler;

import java.util.HashMap;
import java.util.Map;

/**
 * 脚本执行结果类
 * code 取值
 *  0-执行成功，理应有结果
 *  1-执行结果为空
 *  -1-失败
 */
public class CommonScriptResult {
    private int code;
    private String msg;
    // 存储脚本执行的结果值，data中的value的类型只能是String或Map<String,Object>
    private Map<String, Object> data;

    // 成功
    private static final int SUCCESS = 0;
    // 执行结果为空
    private static final int EMPTY = 1;
    // 解析失败
    private static final int FAIL = -1;

    /**
     * 表示脚本执行成功，data理应有值
     * @param data
     * @return
     */
    public static CommonScriptResult success(Map<String, Object> data) {
        CommonScriptResult commonScriptResult = new CommonScriptResult();
        commonScriptResult.setCode(SUCCESS);
        commonScriptResult.setMsg("SUCCESS");
        commonScriptResult.setData(data);
        return commonScriptResult;
    }

    public static CommonScriptResult empty() {
        return empty("EMPTY");
    }

    public static CommonScriptResult empty(String message) {
        CommonScriptResult commonScriptResult = new CommonScriptResult();
        commonScriptResult.setCode(EMPTY);
        commonScriptResult.setMsg(message);
        return commonScriptResult;
    }

    public static CommonScriptResult fail(String message) {
        CommonScriptResult commonScriptResult = new CommonScriptResult();
        commonScriptResult.setCode(FAIL);
        commonScriptResult.setMsg(message);
        return commonScriptResult;
    }

    public boolean isSuccess() {
        return this.code == SUCCESS;
    }

    public boolean isEmpty() {
        return this.code == EMPTY;
    }

    public boolean isFail() {
        return this.code < SUCCESS;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}
